# ./addons/eventscripts/corelib/damage/damage.py
# ported to Python by Hunter
# original command by GODJonez

#   damage <target user> <damage> [<damage type> [<source userid>]]
#     target user = the player(s) to be hurt,
#       supports foreach player flags, if preceeded with !, makes source userid immune, eg. !#all
#     damage = the amount of damage to do to player
#     damage type = (OPTIONAL) the type of the damage (number or "explosion")
#     source userid = (OPTIONAL) the player that causes the damage
#       REQUIRED when using user flags! Otherwise a random user gets credit...
#
# DAMAGE TYPES (if not "explosion")
# 0       GENERIC
# 1       CRUSH
# 2       BULLET
# 4       SLASH
# 8       BURN
# 16      FREEZE
# 32      FALL   (recommended for CSS!)
# 64      BLAST
# 128     CLUB
# 256     SHOCK
# 512     SONIC
# 1024    ENERGYBEAM
# 16384   DROWN
# 32768   PARALYSE
# 65536   NERVEGAS
# 131072  POISON
# 262144  RADIATION
# 524288  DROWNRECOVER
# 1048576 CHEMICAL
# 2097152 SLOWBURN
# 4194304 SLOWFREEZE

import es
import cmdlib

import playerlib

def load():
    cmdlib.registerServerCommand('damage', damage_cmd, 'Syntax: damage <target user> <damage> [<damage type> [<source userid>]]')

def unload():
    cmdlib.unregisterServerCommand('damage')

def damage_cmd():
    if len(args) > 1:
        target = args[0]
        damage = args[1]
        userid = es.getuserid(target.strip('!'))
        identifier = target.strip('!')
        if len(args) > 2:
            damagetype = args[2]
        else:
            damagetype = 32
        if len(args) > 3:
            source = es.getuserid(args[3])
        else:
            source = None
        if '#' in identifier or userid:
            if '#' in identifier:
                identifier = identifier.replace('#', ',#')[1:]
                for userid in playerlib.getUseridList(identifier):
                    if not source: source = userid
                    es.server.cmd('es_xfire %s !self addoutput "targetname es_damage"' % userid)
            elif userid:
                if not source: source = userid
                es.server.cmd('es_xfire %s !self addoutput "targetname es_damage"' % userid)
            if target[:1] == '!' and len(args) > 3:
                es.server.cmd('es_xfire %s !self addoutput "targetname es_human"' % source)
            if source:
                es.server.cmd('es_xfire %s point_hurt Kill' % source)
                es.server.cmd('es_xtrick entity point_hurt')
                es.server.cmd('es_xfire %s point_hurt addoutput "Damage %s"' % (source, damage))
                es.server.cmd('es_xfire %s point_hurt addoutput "DamageType %s"' % (source, damagetype))
                es.server.cmd('es_xfire %s point_hurt addoutput "DamageTarget es_damage"' % source)
                es.server.cmd('es_xfire %s point_hurt Hurt' % source)
                es.server.cmd('es_xfire %s player addoutput "targetname es_human"' % source)
            else:
                es.dbgmsg(0, 'damage: Invalid source user "%s" to damage.' % source)
        else:
            es.dbgmsg(0, 'damage: Invalid target user "%s" to damage.' % target)
    else:
        es.dbgmsg(0, 'damage: Not enough arguments to damage. Syntax: damage <target user> <damage> [<damage type> [<source userid>]]')
